//删除所有cookies
function clearCookies(){
    var cookies = document.cookie.split(";");
    for ( var i = 0; i < cookies.length; i++) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        var domain = location.host.substr(location.host.indexOf('.'));
        document.cookie = name
            + "=;expires="+ exp.toGMTString();
    }
}
//删除cookies
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires="
            + exp.toGMTString();
}
//获取cookie
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length
            }
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}
//设置cookie
function setCookie(c_name, value, expiredays) {
    //不设置时间，默认保存一个月
    if(expiredays == null){
        expiredays = 30;
    }else if(expiredays == 0){}{
        //为0，则是当前浏览器
        expiredays = null;
    }
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name
        + "="
        + escape(value)
        + ((expiredays == null) ? "" : ";expires="
            + exdate.toGMTString());
}
//检测cookie是否存在
function checkCookie(name) {
    var username = getCookie(name);
    if (username == null || username == "") {
        return false;
    } else {
        return true;
    }
}

/**
 * utf8编码
 * @param str
 * @returns {string}
 */
function encodeUTF8(str){
    var temp = "",rs = "";
    for( var i=0 , len = str.length; i < len; i++ ){
        temp = str.charCodeAt(i).toString(16);
        rs  += "\\u"+ new Array(5-temp.length).join("0") + temp;
    }
    return rs;
}
/**
 * utf8解码
 * @param str
 * @returns {XML|*|void|string}
 */
function decodeUTF8(str){
    return str.replace(/(\\u)(\w{4}|\w{2})/gi, function($0,$1,$2){
        return String.fromCharCode(parseInt($2,16));
    });
}

/**
 * 加入购物车
 * @param pid 产品ID
 * @param spid 规格ID
 * @param num 数量
 * @param attr 属性
 */
function addToCart(pid,spid,num,attr){
    var date = formatDate(new Date(),"yyyy-MM-dd HH:mm:ss");
    //各个属性进行排序
    attr = formatCartAttr(attr);
    //属性进行编码
    attr = encodeUTF8(attr);
    var cart = getCookie("cart");
    if(cart == null || cart.length <= 0){
        //之前无，直接加
        var arr = [];
        var obj = {};
        obj.pid = pid;
        obj.spid = spid;
        obj.num = num;
        obj.attr = attr;
        obj.date = date;
        arr.push(obj);
        var arrStr = JSON.stringify(arr);
        setCookie("cart",encodeUTF8(arrStr));
    }else{
        //之前有，处理后加
        var arrStr = decodeUTF8(cart);
        var arr = JSON.parse(arrStr);
        var has = false;
        for(var i = 0;i < arr.length;i++){
            var obj = arr[i];
            if(obj.pid == pid && obj.spid == spid && obj.attr == attr){
                obj.num = obj.num + num;
                has = true;
                break;
            }
        }
        if(!has){
            var obj = {};
            obj.pid = pid;
            obj.spid = spid;
            obj.num = num;
            obj.attr = attr;
            obj.date = date;
            arr.push(obj);
        }
        var arrStr = JSON.stringify(arr);
        setCookie("cart",encodeUTF8(arrStr));
    }
}
/**
 * 从购物车中删除某个产品
 * @param pid
 * @param spid
 */
function deleteFromCart(pid,spid){
    var cart = getCookie("cart");
    var arrStr = decodeUTF8(cart);
    var arr = JSON.parse(arrStr);
    for(var i = 0;i < arr.length;i++){
        var obj = arr[i];
        if(obj.pid == pid && obj.spid == spid){
            arr = arr.remove(obj);
            break;
        }
    }
    var arrStr = JSON.stringify(arr);
    setCookie("cart",encodeUTF8(arrStr));
}
/**
 * 清空购物车
 */
function clearCart(){
    delCookie("cart");
}